package com.hainatong.shiphelper.module.cert;

import android.view.View;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hainatong.shiphelper.MvvmLazyFragment;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.FragmentCertChangeBinding;
import com.hainatong.shiphelper.mainhome.adapter.ProcessStepAdapter;
import com.hainatong.shiphelper.module.cert.entity.CertChangeParam;
import com.hainatong.shiphelper.module.cert.viewmodel.CertViewModel;
import com.hainatong.shiphelper.utils.TimeUtil;
import com.hainatong.shiphelper.widget.PickerDialog;
import com.hainatong.shiphelper.widget.datapicker.CustomDatePicker;

public class ChangeFragment extends MvvmLazyFragment<FragmentCertChangeBinding, CertViewModel>
{
    private CustomDatePicker datePicker;
    private PickerDialog pickerDialog;

    private CertChangeParam changeParam;

    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_cert_change;
    }

    @Override
    public int getBindingVariable()
    {
        return 0;
    }

    @Override
    protected void onRetryBtnClick()
    {
    }

    @Override
    protected void initView(View view)
    {
        changeParam = new CertChangeParam();
        pickerDialog = new PickerDialog(getActivity(), (selectStr, position) -> {

            viewDataBinding.tvChangeType.setText( selectStr );
            ToastUtils.showShort( selectStr );
            if(selectStr.equals("正常更换"))
            {
                changeParam.setChange_type( 1 );
            }
            else if( selectStr.equals("延期更换") )
            {
                changeParam.setChange_type( 2 );
            }

        });
        initDataPicker();
        viewDataBinding.btnSelectTime.setOnClickListener( v -> {
            datePicker.show( TimeUtil.getTimeStandard( System.currentTimeMillis() ) );
        } );

        viewDataBinding.btnSelectChangeType.setOnClickListener( v -> {

            pickerDialog.showPickContent(new String[]{"正常更换","延期更换"});

        } );

        ProcessStepAdapter stepAdapter = ProcessStepAdapter.newInstance( new String[]{"审批人","审批人","审批人"}, new String[]{"大副","船长","岸基"} );
        viewDataBinding.stepRecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
        viewDataBinding.stepRecyclerview.hasFixedSize();
        viewDataBinding.stepRecyclerview.setAdapter( stepAdapter );

        //提交 更换
        viewDataBinding.btnSubmit.setOnClickListener( v -> {

            changeParam.setStatus(1);

            viewModel.submitChangeCertTask(changeParam );

        } );

    }


    private void initDataPicker()
    {
        datePicker = new CustomDatePicker(getActivity(), new CustomDatePicker.ResultHandler() {
            @Override
            public void handle(String time)
            {
                viewDataBinding.tvChangeTime.setText(  TimeUtil.getTimeYMDHS2YMD( time )  );

                // 获取时间 一个long值
                //long resultTime = TimeUtil.parseDate(time).getTime();
                LogUtils.e("CustomDatePicker", time + "  时间= " + TimeUtil.getTimeYMDHS2YMD( time ));
            }
        } , CustomDatePicker.getTimeStandard( System.currentTimeMillis() ) ,  TimeUtil.getTimeStandard((System.currentTimeMillis() + 1000*60*60*24*365*5)));

        // 初始化日期格式请用：yyyy-MM-dd HH:mm
        // HH:mm，否则不能正常运行 //false 不显示时和分
        datePicker.showSpecificTime(true);
        datePicker.setIsLoop(true); // false 不允许循环滚动
        /*
         * //currentTime.setText(now); datePicker = new CustomDatePicker(this, new
         * CustomDatePicker.ResultHandler() {
         * @Override public void handle(String time) { // 回调接口，获得选中的时间 //currentTime.setText(time);
         * } }, "2010-01-01 00:00", now); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行
         * datePicker.showSpecificTime(true); // 显示时和分 datePicker.setIsLoop(true); // 允许循环滚动
         */
    }


    @Override
    protected void lazyLoad()
    {
        //提交 结果
        viewModel.getCertChangeResult().observe( this,result -> {


        } );


    }
}
